//获取会议列表
//数据格式


/*card所需内容
{
    meet_id: int;//序号
	coverImage: string;//封面图路径
    title: string;//标题
	time: string;//时间
	location: string;//地点
    date: string;//日期
    type: string;//类型
	playback：string//仅线下/观看回放
},
*/

export const getFilteredMeetings = async (params) => {
  try {
    console.log('发送请求参数:', params);
    
    const response = await uni.request({
      url: 'http://localhost:8004/api/v2/att/meet/card',
      method: 'POST',
      data: {
        type: params.type === 'ALL' ? '' : params.type,
        name: params.keyword || '',
        time: params.date === 'ALL' ? '' : params.date,
      },
      header: { 
        'Content-Type': 'application/json',
        'token': token
      },
    });

    // 打印服务器返回的结果
    console.log('服务器返回结果:', response.data);

    if (response.statusCode === 200) {
      // 确保返回的是数组
      const result = response.data;
      if (Array.isArray(result)) {
        return result;
      } else if (result && Array.isArray(result.data)) {
        return result.data;
      } else {
        console.warn('服务器返回的数据格式不是数组:', result);
        return [];
      }
    } else {
      throw new Error(`请求失败：${response.data?.message || '未知错误'}`);
    }
  } catch (error) {
    console.error('请求失败:', error);
    // 返回空数组而不是抛出错误，避免前端崩溃
    uni.showToast({
      title: '获取会议列表失败',
      icon: 'none'
    });
    return [];
  }
};

/*
export const getFilteredMeetings = async (params) => {
  try {
    // ✅ 模拟后端返回的数据
    if (process.env.NODE_ENV === 'development') {
      console.log('模拟请求参数:', params);
      return Promise.resolve([
        {
          meetId: 1,
          title: "示例主论坛会议",
          date: "2025-04-19", // 确保日期格式与你的逻辑一致
          time: "10:00",
          location: "主会场A",
          coverImage: "https://placehold.co/400x200", // 使用占位图片
          playback: "回放"
        },
        {
          meetId: 2,
          title: "示例平行会议",
          date: "2025-04-25",
          time: "14:00",
          location: "分会场B",
          coverImage: "https://placehold.co/400x200", // 使用占位图片
          playback: "录播"
        },
        {
          meetId: 3,
          title: "示例新品发布会",
          date: "2025-05-08",
          time: "09:00",
          location: "线上",
          coverImage: "https://placehold.co/400x200", // 使用占位图片
          playback: "直播"
        }
      ]);
    }

    // ⬇️ 你的原始代码
    const response = await uni.request({
      url: 'http://localhost:8004/api/v2/att/meet/card',
      method: 'POST',
      data: {
        type: params.type === 'ALL' ? '' : params.type,
        time: params.date === 'ALL' ? '' : params.date,
        name: params.keyword || '',
      },
      header: { 'Content-Type': 'application/json' },
    });

    // 打印服务器返回的结果
    console.log('服务器返回结果:', response.data);

    if (response.statusCode === 200) {
      return response.data.data; // 假设服务器返回的数据在 response.data.data 中
    } else {
      throw new Error(`请求失败：${response.data?.message || '未知错误'}`);
    }
  } catch (error) {
    console.error('请求失败:', error);
    throw error;
  }
};
*/
const token = uni.getStorageSync('token');
// const token = 'meetingassistanteyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cGNNZWV0aW5nYXNzaXN0YW50V2l0aEZpdmVQZXJzb25XaGljaElzVmVyeUdvb2QiLCJpZCI6MTEwOTUxMzkzOTE5Mzk1NDMwNCwidXNlcm5hbWUiOiJFSSIsInBob25lIjoiMTgzMjUwNDMxNzciLCJpYXQiOjE3NDM0NzQ2MjgsImV4cCI6MTc0NDA3OTQyOH0.9SnBY6doEpmqR9Yd1uBi0p5fxcguCqmQYfwznv7lo6g';

// 获取会议详情
export const getMeetingDetail = async (meetId) => {
  try {
    const response = await uni.request({
      url: `http://localhost:8004/api/v2/att/meet/detail`,
      method: 'GET',
      data: {
        meetId: meetId
      },
      header: { 
        'Content-Type': 'application/json',
        'token': token
       },
    });

    // 打印服务器返回的结果
    console.log('会议详情返回结果:', response.data);

    if (response.statusCode === 200) {
      return response.data; // 返回会议详情数据
    } else {
      throw new Error(`请求失败：${response.data?.message || '未知错误'}`);
    }
  } catch (error) {
    console.error('获取会议详情失败:', error);
    throw error;
  }
};

// 获取会议文件列表
export const getMeetingFiles = async (meetId) => {
  try {
    const response = await uni.request({
      url: `http://localhost:8004/api/v2/att/meet/file`,
      method: 'GET',
      data: {
        meetId: meetId
      },
      header: { 
        'Content-Type': 'application/json',
        'token': token
      },
    });

    // 打印服务器返回的结果
    console.log('会议文件列表返回结果:', response.data);

    if (response.statusCode === 200) {
      return response.data; // 返回文件列表数据
    } else {
      throw new Error(`请求失败：${response.data?.message || '未知错误'}`);
    }
  } catch (error) {
    console.error('获取会议文件列表失败:', error);
    throw error;
  }
};

// 订阅会议
export const subscribeMeeting = async (meetId) => {
  try {
    const response = await uni.request({
      url: `http://localhost:8004/api/v2/att/meet/subscribe`,
      method: 'POST',
      data: {
        meetId: meetId
      },
      header: { 
        'Content-Type': 'application/json',
        'token': token
      },
    });

    // 打印服务器返回的结果
    console.log('订阅会议返回结果:', response.data);

    if (response.statusCode === 200) {
      return {
        success: true,
        data: response.data
      };
    } else {
      return {
        success: false,
        message: response.data?.message || '订阅失败'
      };
    }
  } catch (error) {
    console.error('订阅会议失败:', error);
    return {
      success: false,
      message: error.message || '网络错误'
    };
  }
};




